home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Magazine / GraphicsCards / StormMesa / src / MGL / halftone.c next >
C/C++ Source or Header  |  1999-02-04  |  18KB  |  407 lines

  1. /****************************************************************************
  2. *
  3. *                      Mesa bindings for SciTech MGL
  4. *
  5. *               Copyright (C) 1996-1998 SciTech Software, Inc.
  6. *                            All rights reserved.
  7. *
  8. * Language:        ANSI C
  9. * Environment:    IBM PC (MS DOS)
  10. *
  11. * Description:    Default HalfTone palette and associated lookup tables
  12. *                for performing fast halftone dithering.
  13. *
  14. *                The halftone palette is set up to use entries from 20 to
  15. *                226 in the physical palette, leaving the top 20 and bottom
  16. *                20 for operating system specific use.
  17. *
  18. * This library is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU Library General Public
  20. * License as published by the Free Software Foundation; either
  21. * version 2 of the License, or (at your option) any later version.
  22. *
  23. * This library is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  26. * Library General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Library General Public
  29. * License along with this library; if not, write to the Free
  30. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  31. *
  32. ****************************************************************************/
  33.  
  34. #include "mgraph.h"
  35. #include "mgl\halftone.h"
  36. #include <string.h>
  37.  
  38. /*------------------------- Implementation --------------------------------*/
  39.  
  40. /****************************************************************************
  41. DESCRIPTION:
  42. Returns a copy of the MGL halftone palette.
  43.  
  44. HEADER:
  45. mgraph.h
  46.  
  47. PARAMETERS:
  48. pal    - Place to store the halftone palette values
  49.  
  50. REMARKS:
  51. This function copies the MGL halftone palette into the specified palette structure.
  52. The halftone palette always contains 256 colors, and hence the palette array must
  53. contain 256 palette entries. This palette is a special palette used by MGL when
  54. running in RGB dithered rasterizing mode for 8 bit video modes. If you intend to
  55. enable RGB dithering with the MGL_setColorMapMode function, you must get a
  56. copy of the halftone palette and program the hardware palette for your display
  57. device or windowed device to be the same as this halftone palette.
  58.  
  59. Note that the MGL halftone palette is compatible with the standard Windows
  60. halftone palette, so you can perform 8 bit dithering operations in a window without
  61. needing to go into SYSPAL_STATIC mode.
  62.  
  63. SEE ALSO:
  64. MGL_setPalette, MGL_realizePalette, MGL_setColorMapMode
  65. ****************************************************************************/
  66. void MGLAPI MGL_getHalfTonePalette(
  67.     palette_t *pal)
  68. { memcpy(pal,_MGL_halftonePal,sizeof(palette_t) * 256); }
  69.  
  70. /****************************************************************************
  71. DESCRIPTION:
  72. Compute color for pixel in halftone palette.
  73.  
  74. HEADER:
  75. mgraph.h
  76.  
  77. PARAMETERS:
  78. x    - x pixel coordinate
  79. y    - y pixel coordinate
  80. R    - Red component for pixel color
  81. G    - Green component for pixel color
  82. B    - Blue component for pixel color
  83.  
  84. RETURNS:
  85. Color index for the pixel in MGL halftone palette
  86.  
  87. REMARKS:
  88. This function computes the color index for a specified 24 bit RGB pixel value in the
  89. standard MGL halftone palette, and can be used to perform real-time dithering of
  90. pixel values from RGB colors to color index colors. This routine uses a fast table
  91. lookup and an 8x8 ordered dither to do the conversion.
  92.  
  93. SEE ALSO:
  94. MGL_getHalfTonePalette
  95. ****************************************************************************/
  96. uchar MGLAPI MGL_halfTonePixel(
  97.     int x,
  98.     int y,
  99.     uchar R,
  100.     uchar G,
  101.     uchar B)
  102. {
  103.     uchar dither = _MGL_dither8x8[((x & 7) << 3) + (y & 7)];
  104.     return 20 +
  105.         _MGL_div51[R] + (_MGL_mod51[R] > dither) +
  106.         _MGL_mul6[_MGL_div51[G] + (_MGL_mod51[G] > dither)] +
  107.         _MGL_mul36[_MGL_div51[B] + (_MGL_mod51[B] > dither)];
  108. }
  109.  
  110. /***************************************************************************
  111. DESCRIPTION:
  112. Compute color for an 555 format RGB pixel using a halftone dither
  113.  
  114. HEADER:
  115. mgraph.h
  116.  
  117. PARAMETERS:
  118. x    - x coordinate of pixel (need only be relative)
  119. y    - y coordinate of pixel (need only be relative)
  120. R    - R value for pixel (8 bit components)
  121. G    - G value for pixel (8 bit components)
  122. B    - B value for pixel (8 bit components)
  123.  
  124. RETURNS:
  125. Packed RGB 555 format pixel color
  126.  
  127. REMARKS:
  128. This function computes the color of an RGB 555 format pixel using a fast
  129. halftone dither algorithm. The resulting color is pre-packed into the
  130. correct framebuffer format as part of the dithering process.
  131.  
  132. SEE ALSO:
  133. MGL_halfTonePixel565
  134. ***************************************************************************/
  135. ushort MGLAPI MGL_halfTonePixel555(
  136.     int x,
  137.     int y,
  138.     uchar R,
  139.     uchar G,
  140.     uchar B)
  141. {
  142.     uchar _dither = _MGL_dither4x4[(((y) & 3) << 2) + ((x) & 3)];
  143.     return (ushort)
  144.        ((((ulong)_MGL_div8[R] + (_MGL_mod8[R] > _dither)) << 10) +
  145.         (((ulong)_MGL_div8[G] + (_MGL_mod8[G] > _dither)) << 5) +
  146.         (((ulong)_MGL_div8[B] + (_MGL_mod8[B] > _dither)) << 0));
  147. }
  148.  
  149. /***************************************************************************
  150. DESCRIPTION:
  151. Compute color for an 565 format RGB pixel using a halftone dither
  152.  
  153. HEADER:
  154. mgraph.h
  155.  
  156. PARAMETERS:
  157. x    - x coordinate of pixel (need only be relative)
  158. y    - y coordinate of pixel (need only be relative)
  159. R    - R value for pixel (8 bit components)
  160. G    - G value for pixel (8 bit components)
  161. B    - B value for pixel (8 bit components)
  162.  
  163. RETURNS:
  164. Packed RGB 565 format pixel color
  165.  
  166. REMARKS:
  167. This function computes the color of an RGB 565 format pixel using a fast
  168. halftone dither algorithm. The resulting color is pre-packed into the
  169. correct framebuffer format as part of the dithering process.
  170.  
  171. SEE ALSO:
  172. MGL_halfTonePixel555
  173. ***************************************************************************/
  174. ushort MGLAPI MGL_halfTonePixel565(
  175.     int x,
  176.     int y,
  177.     uchar R,
  178.     uchar G,
  179.     uchar B)
  180. {
  181.     uchar _dither = _MGL_dither4x4[(((y) & 3) << 2) + ((x) & 3)];
  182.     return (ushort)
  183.        ((((ulong)_MGL_div8[R] + (_MGL_mod8[R] > _dither)) << 11) +
  184.         (((ulong)_MGL_div4[G] + (_MGL_mod4[G] > (_dither>>1))) << 5) +
  185.         (((ulong)_MGL_div8[B] + (_MGL_mod8[B] > _dither)) << 0));
  186. }
  187.  
  188. /*------------------------- Lookup Tables ---------------------------------*/
  189.  
  190. /* Default windows compatible halftone palette. This includes the default
  191.  * Windows system colors in the first 10 and last 10 entries in the
  192.  * palette.
  193.  */
  194.  
  195. palette_t _VARAPI _MGL_halftonePal[256] = {
  196.     {0x00,0x00,0x00,0}, {0xA8,0x00,0x00,0}, {0x00,0xA8,0x00,0}, {0xA8,0xA8,0x00,0},
  197.     {0x00,0x00,0xA8,0}, {0xA8,0x00,0xA8,0}, {0x00,0x54,0xA8,0}, {0xA8,0xA8,0xA8,0},
  198.     {0x54,0x54,0x54,0}, {0xFC,0x54,0x54,0}, {0x54,0xFC,0x54,0}, {0xFC,0xFC,0x54,0},
  199.     {0x54,0x54,0xFC,0}, {0xFC,0x54,0xFC,0}, {0x54,0xFC,0xFC,0}, {0xFC,0xFC,0xFC,0},
  200.     {0x00,0x00,0x00,0}, {0x14,0x14,0x14,0}, {0x20,0x20,0x20,0}, {0x2C,0x2C,0x2C,0},
  201.     {0x00,0x00,0x00,0}, {0x00,0x00,0x33,0}, {0x00,0x00,0x66,0}, {0x00,0x00,0x99,0},
  202.     {0x00,0x00,0xCC,0}, {0x00,0x00,0xFF,0}, {0x00,0x33,0x00,0}, {0x00,0x33,0x33,0},
  203.     {0x00,0x33,0x66,0}, {0x00,0x33,0x99,0}, {0x00,0x33,0xCC,0}, {0x00,0x33,0xFF,0},
  204.     {0x00,0x66,0x00,0}, {0x00,0x66,0x33,0}, {0x00,0x66,0x66,0}, {0x00,0x66,0x99,0},
  205.     {0x00,0x66,0xCC,0}, {0x00,0x66,0xFF,0}, {0x00,0x99,0x00,0}, {0x00,0x99,0x33,0},
  206.     {0x00,0x99,0x66,0}, {0x00,0x99,0x99,0}, {0x00,0x99,0xCC,0}, {0x00,0x99,0xFF,0},
  207.     {0x00,0xCC,0x00,0}, {0x00,0xCC,0x33,0}, {0x00,0xCC,0x66,0}, {0x00,0xCC,0x99,0},
  208.     {0x00,0xCC,0xCC,0}, {0x00,0xCC,0xFF,0}, {0x00,0xFF,0x00,0}, {0x00,0xFF,0x00,0},
  209.     {0x00,0xFF,0x66,0}, {0x00,0xFF,0x99,0}, {0x00,0xFF,0xCC,0}, {0x00,0xFF,0xFF,0},
  210.     {0x33,0x00,0x00,0}, {0x33,0x00,0x33,0}, {0x33,0x00,0x66,0}, {0x33,0x00,0x99,0},
  211.     {0x33,0x00,0xCC,0}, {0x33,0x00,0xFF,0}, {0x33,0x33,0x00,0}, {0x33,0x33,0x33,0},
  212.     {0x33,0x33,0x66,0}, {0x33,0x33,0x99,0}, {0x33,0x33,0xCC,0}, {0x33,0x33,0xFF,0},
  213.     {0x33,0x66,0x00,0}, {0x33,0x66,0x33,0}, {0x33,0x66,0x66,0}, {0x33,0x66,0x99,0},
  214.     {0x33,0x66,0xCC,0}, {0x33,0x66,0xFF,0}, {0x33,0x99,0x00,0}, {0x33,0x99,0x33,0},
  215.     {0x33,0x99,0x66,0}, {0x33,0x99,0x99,0}, {0x33,0x99,0xCC,0}, {0x33,0x99,0xFF,0},
  216.     {0x33,0xCC,0x00,0}, {0x33,0xCC,0x33,0}, {0x33,0xCC,0x66,0}, {0x33,0xCC,0x99,0},
  217.     {0x33,0xCC,0xCC,0}, {0x33,0xCC,0xFF,0}, {0x00,0xFF,0x00,0}, {0x33,0xFF,0x33,0},
  218.     {0x33,0xFF,0x66,0}, {0x33,0xFF,0x99,0}, {0x33,0xFF,0xCC,0}, {0x33,0xFF,0xFF,0},
  219.     {0x66,0x00,0x00,0}, {0x66,0x00,0x33,0}, {0x66,0x00,0x66,0}, {0x66,0x00,0x99,0},
  220.     {0x66,0x00,0xCC,0}, {0x66,0x00,0xFF,0}, {0x66,0x33,0x00,0}, {0x66,0x33,0x33,0},
  221.     {0x66,0x33,0x66,0}, {0x66,0x33,0x99,0}, {0x66,0x33,0xCC,0}, {0x66,0x33,0xFF,0},
  222.     {0x66,0x66,0x00,0}, {0x66,0x66,0x33,0}, {0x66,0x66,0x66,0}, {0x66,0x66,0x99,0},
  223.     {0x66,0x66,0xCC,0}, {0x66,0x66,0xFF,0}, {0x66,0x99,0x00,0}, {0x66,0x99,0x33,0},
  224.     {0x66,0x99,0x66,0}, {0x66,0x99,0x99,0}, {0x66,0x99,0xCC,0}, {0x66,0x99,0xFF,0},
  225.     {0x66,0xCC,0x00,0}, {0x66,0xCC,0x33,0}, {0x66,0xCC,0x66,0}, {0x66,0xCC,0x99,0},
  226.     {0x66,0xCC,0xCC,0}, {0x66,0xCC,0xFF,0}, {0x66,0xFF,0x00,0}, {0x66,0xFF,0x33,0},
  227.     {0x66,0xFF,0x66,0}, {0x66,0xFF,0x99,0}, {0x66,0xFF,0xCC,0}, {0x66,0xFF,0xFF,0},
  228.     {0x99,0x00,0x00,0}, {0x99,0x00,0x33,0}, {0x99,0x00,0x66,0}, {0x99,0x00,0x99,0},
  229.     {0x99,0x00,0xCC,0}, {0x99,0x00,0xFF,0}, {0x99,0x33,0x00,0}, {0x99,0x33,0x33,0},
  230.     {0x99,0x33,0x66,0}, {0x99,0x33,0x99,0}, {0x99,0x33,0xCC,0}, {0x99,0x33,0xFF,0},
  231.     {0x99,0x66,0x00,0}, {0x99,0x66,0x33,0}, {0x99,0x66,0x66,0}, {0x99,0x66,0x99,0},
  232.     {0x99,0x66,0xCC,0}, {0x99,0x66,0xFF,0}, {0x99,0x99,0x00,0}, {0x99,0x99,0x33,0},
  233.     {0x99,0x99,0x66,0}, {0x99,0x99,0x99,0}, {0x99,0x99,0xCC,0}, {0x99,0x99,0xFF,0},
  234.     {0x99,0xCC,0x00,0}, {0x99,0xCC,0x33,0}, {0x99,0xCC,0x66,0}, {0x99,0xCC,0x99,0},
  235.     {0x99,0xCC,0xCC,0}, {0x99,0xCC,0xFF,0}, {0x99,0xFF,0x00,0}, {0x99,0xFF,0x33,0},
  236.     {0x99,0xFF,0x66,0}, {0x99,0xFF,0x99,0}, {0x99,0xFF,0xCC,0}, {0x99,0xFF,0xFF,0},
  237.     {0xCC,0x00,0x00,0}, {0xCC,0x00,0x33,0}, {0xCC,0x00,0x66,0}, {0xCC,0x00,0x99,0},
  238.     {0xCC,0x00,0xCC,0}, {0xCC,0x00,0xFF,0}, {0xCC,0x33,0x00,0}, {0xCC,0x33,0x33,0},
  239.     {0xCC,0x33,0x66,0}, {0xCC,0x33,0x99,0}, {0xCC,0x33,0xCC,0}, {0xCC,0x33,0xFF,0},
  240.     {0xCC,0x66,0x00,0}, {0xCC,0x66,0x33,0}, {0xCC,0x66,0x66,0}, {0xCC,0x66,0x99,0},
  241.     {0xCC,0x66,0xCC,0}, {0xCC,0x66,0xFF,0}, {0xCC,0x99,0x00,0}, {0xCC,0x99,0x33,0},
  242.     {0xCC,0x99,0x66,0}, {0xCC,0x99,0x99,0}, {0xCC,0x99,0xCC,0}, {0xCC,0x99,0xFF,0},
  243.     {0xCC,0xCC,0x00,0}, {0xCC,0xCC,0x33,0}, {0xCC,0xCC,0x66,0}, {0xCC,0xCC,0x99,0},
  244.     {0xCC,0xCC,0xCC,0}, {0xCC,0xCC,0xFF,0}, {0xCC,0xFF,0x00,0}, {0xCC,0xFF,0x33,0},
  245.     {0xCC,0xFF,0x66,0}, {0xCC,0xFF,0x99,0}, {0xCC,0xFF,0xCC,0}, {0xCC,0xFF,0xFF,0},
  246.     {0xFF,0x00,0x00,0}, {0xFF,0x00,0x00,0}, {0xFF,0x00,0x66,0}, {0xFF,0x00,0x99,0},
  247.     {0xFF,0x00,0xCC,0}, {0xFF,0x00,0xFF,0}, {0xFF,0x00,0x00,0}, {0xFF,0x33,0x33,0},
  248.     {0xFF,0x33,0x66,0}, {0xFF,0x33,0x99,0}, {0xFF,0x33,0xCC,0}, {0xFF,0x33,0xFF,0},
  249.     {0xFF,0x66,0x00,0}, {0xFF,0x66,0x33,0}, {0xFF,0x66,0x66,0}, {0xFF,0x66,0x99,0},
  250.     {0xFF,0x66,0xCC,0}, {0xFF,0x66,0xFF,0}, {0xFF,0x99,0x00,0}, {0xFF,0x99,0x33,0},
  251.     {0xFF,0x99,0x66,0}, {0xFF,0x99,0x99,0}, {0xFF,0x99,0xCC,0}, {0xFF,0x99,0xFF,0},
  252.     {0xFF,0xCC,0x00,0}, {0xFF,0xCC,0x33,0}, {0xFF,0xCC,0x66,0}, {0xFF,0xCC,0x99,0},
  253.     {0xFF,0xCC,0xCC,0}, {0xFF,0xCC,0xFF,0}, {0xFF,0xFF,0x00,0}, {0xFF,0xFF,0x33,0},
  254.     {0xFF,0xFF,0x66,0}, {0xFF,0xFF,0x99,0}, {0xFF,0xFF,0xCC,0}, {0xFF,0xFF,0xFF,0},
  255.     {0x2C,0x40,0x40,0}, {0x2C,0x40,0x3C,0}, {0x2C,0x40,0x34,0}, {0x2C,0x40,0x30,0},
  256.     {0x2C,0x40,0x2C,0}, {0x30,0x40,0x2C,0}, {0x34,0x40,0x2C,0}, {0x3C,0x40,0x2C,0},
  257.     {0x40,0x40,0x2C,0}, {0x40,0x3C,0x2C,0}, {0x40,0x34,0x2C,0}, {0x40,0x30,0x2C,0},
  258.     {0x54,0x54,0x54,0}, {0xFC,0x54,0x54,0}, {0x54,0xFC,0x54,0}, {0xFC,0xFC,0x54,0},
  259.     {0x54,0x54,0xFC,0}, {0xFC,0x54,0xFC,0}, {0x54,0xFC,0xFC,0}, {0xFC,0xFC,0xFC,0},
  260.     };
  261.  
  262. /* Division lookup tables.  These tables compute 0-255 divided by 51 and
  263.  * modulo 51.  These tables could approximate gamma correction.
  264.  */
  265.  
  266. uchar _VARAPI _MGL_div51[256] = {
  267.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  268.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  269.     0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  270.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  271.     1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  272.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  273.     2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  274.     3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  275.     3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  276.     4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  277.     4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5,
  278.     };
  279.  
  280. uchar _VARAPI _MGL_mod51[256] = {
  281.     0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  282.     20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
  283.     38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 0, 1, 2, 3, 4, 5, 6,
  284.     7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
  285.     26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
  286.     44, 45, 46, 47, 48, 49, 50, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
  287.     13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
  288.     31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
  289.     49, 50, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
  290.     18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
  291.     36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 0, 1, 2, 3,
  292.     4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
  293.     23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  294.     41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 0,
  295.     };
  296.  
  297. /* Multiplication lookup tables. These compute 0-5 times 6 and 36. */
  298.  
  299. uchar _VARAPI _MGL_mul6[6] = {
  300.     0, 6, 12, 18, 24, 30
  301.     };
  302.  
  303. uchar _VARAPI _MGL_mul36[6] = {
  304.     0, 36, 72, 108, 144, 180
  305.     };
  306.  
  307. /* Ordered 8x8 dither matrix for 8 bit to 2.6 bit halftones. */
  308.  
  309. uchar _VARAPI _MGL_dither8x8[64] = {
  310.      0, 38,  9, 47,  2, 40, 11, 50,
  311.     25, 12, 35, 22, 27, 15, 37, 24,
  312.      6, 44,  3, 41,  8, 47,  5, 43,
  313.     31, 19, 28, 15, 34, 21, 31, 18,
  314.      1, 39, 11, 49,  0, 39, 10, 48,
  315.     27, 14, 36, 23, 26, 13, 35, 23,
  316.      7, 46,  4, 43,  7, 45,  3, 42,
  317.     33, 20, 30, 17, 32, 19, 29, 16,
  318.     };
  319.  
  320. /* Division lookup tables for 16bpp dithering */
  321.  
  322. uchar _VARAPI _MGL_div8[256] = {
  323.     0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,
  324.     2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,
  325.     4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,
  326.     6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,
  327.     8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,
  328.     10,10,10,10,10,10,10,10,11,11,11,11,11,11,11,11,
  329.     12,12,12,12,12,12,12,12,13,13,13,13,13,13,13,13,
  330.     14,14,14,14,14,14,14,14,15,15,15,15,15,15,15,15,
  331.     16,16,16,16,16,16,16,16,17,17,17,17,17,17,17,17,
  332.     18,18,18,18,18,18,18,18,19,19,19,19,19,19,19,19,
  333.     20,20,20,20,20,20,20,20,21,21,21,21,21,21,21,21,
  334.     22,22,22,22,22,22,22,22,23,23,23,23,23,23,23,23,
  335.     24,24,24,24,24,24,24,24,25,25,25,25,25,25,25,25,
  336.     26,26,26,26,26,26,26,26,27,27,27,27,27,27,27,27,
  337.     28,28,28,28,28,28,28,28,29,29,29,29,29,29,29,29,
  338.     30,30,30,30,30,30,30,30,31,31,31,31,31,31,31,31,
  339.     };
  340.  
  341. uchar _VARAPI _MGL_mod8[256] = {
  342.     0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7,
  343.     0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7,
  344.     0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7,
  345.     0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7,
  346.     0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7,
  347.     0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7,
  348.     0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7,
  349.     0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7,
  350.     0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7,
  351.     0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7,
  352.     0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7,
  353.     0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7,
  354.     0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7,
  355.     0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7,
  356.     0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7,
  357.     0,1,2,3,4,5,6,7,0,0,0,0,0,0,0,0,
  358.     };
  359.  
  360. uchar _VARAPI _MGL_div4[256] = {
  361.     0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,
  362.     4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,
  363.     8,8,8,8,9,9,9,9,10,10,10,10,11,11,11,11,
  364.     12,12,12,12,13,13,13,13,14,14,14,14,15,15,15,15,
  365.     16,16,16,16,17,17,17,17,18,18,18,18,19,19,19,19,
  366.     20,20,20,20,21,21,21,21,22,22,22,22,23,23,23,23,
  367.     24,24,24,24,25,25,25,25,26,26,26,26,27,27,27,27,
  368.     28,28,28,28,29,29,29,29,30,30,30,30,31,31,31,31,
  369.     32,32,32,32,33,33,33,33,34,34,34,34,35,35,35,35,
  370.     36,36,36,36,37,37,37,37,38,38,38,38,39,39,39,39,
  371.     40,40,40,40,41,41,41,41,42,42,42,42,43,43,43,43,
  372.     44,44,44,44,45,45,45,45,46,46,46,46,47,47,47,47,
  373.     48,48,48,48,49,49,49,49,50,50,50,50,51,51,51,51,
  374.     52,52,52,52,53,53,53,53,54,54,54,54,55,55,55,55,
  375.     56,56,56,56,57,57,57,57,58,58,58,58,59,59,59,59,
  376.     60,60,60,60,61,61,61,61,62,62,62,62,63,63,63,63,
  377.     };
  378.  
  379. uchar _VARAPI _MGL_mod4[256] = {
  380.     0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,
  381.     0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,
  382.     0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,
  383.     0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,
  384.     0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,
  385.     0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,
  386.     0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,
  387.     0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,
  388.     0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,
  389.     0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,
  390.     0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,
  391.     0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,
  392.     0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,
  393.     0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,
  394.     0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,
  395.     0,1,2,3,0,1,2,3,0,1,2,3,0,0,0,0,
  396.     };
  397.  
  398. /* Ordered 4x4 dither matrix for 8 bit to 5 bit halftones. */
  399.  
  400. uchar _VARAPI _MGL_dither4x4[16] = {
  401.     0, 4, 1, 5,
  402.     6, 2, 7, 3,
  403.     1, 5, 0, 4,
  404.     7, 3, 6, 2,
  405.     };
  406.  
  407.